package com.zt.ftphelper.model;

import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

import java.io.File;

/**
 * @author zt
 * @Description:自定义tree节点
 * @date 15/08/2019 12:52
 */
public class MyTreeItem extends TreeItem<File> {
    private boolean notInitialized = true;
    public MyTreeItem(final File file){
        super(file);
    }
    @Override
    public ObservableList<TreeItem<File>> getChildren(){
        if(notInitialized){
            notInitialized = false;
            if(getValue().isDirectory()){
                for(final File file:getValue().listFiles()){
                    if(file.isDirectory()&&!file.isHidden()){
                        super.getChildren().add(new MyTreeItem(file));
                    }
                }
            }
        }
        return super.getChildren();
    }


    @Override
    public boolean isLeaf(){


        if(getValue().isFile()){
           return true;
        }

        if(getValue().isDirectory()&&!getValue().canRead()){
            return true;
        }

        if(getValue().canRead()&&getValue().listFiles()!=null){
            for(final File file:getValue().listFiles()){
                 if(file.isDirectory()&&file.canRead()){
                     return false;
                 }
            }
            return true;
        }

        return true;
    }

    @Override
    public String toString() {
        return getValue().getPath().toString();
    }
}
